package com.example.liberty.hteach.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import java.lang.ref.WeakReference;

/**
 * Created by Liberty on 2016/9/11.
 */

public class CircleImageView extends ImageView {

    private Paint paint;
    private Xfermode mXfermode=new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
    private WeakReference<Bitmap> mWeakBitmap;

    public CircleImageView(Context context) {
        this(context,null);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint=new Paint();
//        paint.setXfermode(mXfermode);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width=Math.min(getMeasuredWidth(),getMeasuredHeight());
        setMeasuredDimension(width,width);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        Bitmap bitmap=mWeakBitmap==null?null:mWeakBitmap.get();
//        if (bitmap==null||bitmap.isRecycled()){
//            Drawable drawable=getDrawable();
//            if (drawable!=null){
//                bitmap=Bitmap.createBitmap(getWidth(),getHeight()
//                , Bitmap.Config.ARGB_8888);
//                int dWidth=drawable.getIntrinsicWidth();
//                int dHeight=drawable.getIntrinsicHeight();
//                float scale=1.0f;
//                Canvas drawCanvas=new Canvas(bitmap);
//                drawable.setBounds(0,0,(int)(scale*dWidth),(int)(scale*dHeight));
//                drawable.draw(drawCanvas);
//
//            }
//        }
//        if (bitmap!=null){
//            paint.setXfermode(null);
//            canvas.drawBitmap(bitmap,0f,0f,paint);
//        }
        int sc=canvas.saveLayer(0,0,getWidth(),getWidth(),null,Canvas.ALL_SAVE_FLAG);
        Bitmap bitmap;
        Drawable drawable=getDrawable();
        if (drawable instanceof BitmapDrawable){
            bitmap=((BitmapDrawable)drawable).getBitmap();
            canvas.drawBitmap(bitmap,0f,0f,paint);
        }
        paint.setXfermode(mXfermode);
//        paint.setColor(Color.BLACK);
//        paint.setStyle(Paint.Style.FILL);
//        canvas.drawCircle(getWidth()/2,getWidth()/2,getWidth()/4,paint);
        Bitmap circleBitmap=getBitmap();
        canvas.drawBitmap(circleBitmap,0f,0f,paint);
        paint.setXfermode(null);
        canvas.restoreToCount(sc);
    }

    private Bitmap getBitmap(){
        Bitmap bitmap=Bitmap.createBitmap(getWidth(),getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(bitmap);
        Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLACK);
        canvas.drawCircle(getWidth()/2,getWidth()/2,getWidth()/2,paint);
        return bitmap;
    }
}
